package me.sdevil507.supports.word;

import me.sdevil507.biz.fruit.po.FruitPO;
import me.sdevil507.org.po.OrgPo;
import me.sdevil507.supports.status.ComConstant;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

public class ExportWork {

    public ExportWork() {
    }

    public XWPFDocument exportWorkReportData(String exportMonth, OrgPo orgPo, List<FruitPO> list) throws IOException {

        XWPFDocument document = new XWPFDocument();

        //添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText("工作简报");
        titleParagraphRun.setColor("C00000");
        titleParagraphRun.setFontSize(72);

        //换行
        this.addBreak(document);

        //段落
        XWPFParagraph firstParagraph = document.createParagraph();
        XWPFRun run = firstParagraph.createRun();
        run.setText(exportMonth+"刊\r"+orgPo.getNameZh()+"编");
        run.setFontSize(12);
//        //设置段落背景颜色
//        CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
//        cTShd.setVal(STShd.CLEAR);
//        cTShd.setFill("97FFFF");

        //段落  智库研究
        XWPFParagraph zkyj = document.createParagraph();
        //插入下一页
        zkyj.setPageBreak(true);
        XWPFRun zkyjRun = zkyj.createRun();
        zkyjRun.setText("一、智库研究\r");
        zkyjRun.setBold(true);
        zkyjRun.setFontFamily("黑体");
        zkyjRun.setFontSize(16);
        //换行
        this.addBreak(document);

        //1.成果报送及采纳情况
        this.fruitNc(document, list);
        //换行
        this.addBreak(document);
        //2.图书出版
        this.fruitBook(document, list);
        //3.成果摘要、收录及获奖
        this.fruitAward(document, list);
        //4.文章发表（学术文章和媒体文章）
        this.fruitPost(document, list);
        //换行
        this.addBreak(document);
        //5.课题完成
        this.topicData(document, list);

        //段落  智库媒体
        XWPFParagraph zkmt = document.createParagraph();
        XWPFRun zkmtRun = zkmt.createRun();
        zkmtRun.setText("二、智库媒体\r");
        zkmtRun.setBold(true);
        zkmtRun.setFontFamily("黑体");
        zkmtRun.setFontSize(16);
        //换行
        this.addBreak(document);

        //段落  专家访谈
        XWPFParagraph zjft = document.createParagraph();
        XWPFRun zjftRun = zjft.createRun();
        zjftRun.setText("三、专家访谈\r");
        zjftRun.setBold(true);
        zjftRun.setFontFamily("黑体");
        zjftRun.setFontSize(16);
        //换行
        this.addBreak(document);
        //专家访谈
        this.fruitInterview(document, list);

        //段落  智库活动
        XWPFParagraph zkhd = document.createParagraph();
        XWPFRun zkhdRun = zkhd.createRun();
        zkhdRun.setText("四、智库活动\r");
        zkhdRun.setBold(true);
        zkhdRun.setFontFamily("黑体");
        zkhdRun.setFontSize(16);
        //换行
        this.addBreak(document);
        //智库活动
        this.fruitMeeting(document, list);

        //段落 尾言
        XWPFParagraph endP = document.createParagraph();
        //插入下一页
        endP.setPageBreak(true);

        XWPFRun endRun1 = endP.createRun();
        endRun1.setText("智库官网:"+this.getVal(orgPo.getWebsiteAddress()));
        endRun1.setBold(true);
        endRun1.setFontFamily("黑体");
        endRun1.setFontSize(16);

        XWPFParagraph endP2 = document.createParagraph();
        XWPFRun endRun2 = endP2.createRun();
        endRun2.setText("微信公众号:"+this.getVal(orgPo.getOfficialAccounts()));
        endRun2.setBold(true);
        endRun2.setFontFamily("黑体");
        endRun2.setFontSize(16);

        XWPFParagraph endP3 = document.createParagraph();
        XWPFRun endRun3 = endP3.createRun();
        endRun3.setText("联系邮箱:");
        endRun3.setBold(true);
        endRun3.setFontFamily("黑体");
        endRun3.setFontSize(16);

        XWPFParagraph endP4 = document.createParagraph();
        XWPFRun endRun4 = endP4.createRun();
        endRun4.setText("联系电话:");
        endRun4.setBold(true);
        endRun4.setFontFamily("黑体");
        endRun4.setFontSize(16);

        CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
        XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
        //添加页眉
        CTP ctpHeader = CTP.Factory.newInstance();
        CTR ctrHeader = ctpHeader.addNewR();
        CTText ctHeader = ctrHeader.addNewT();
        String headerText = "工作简报 / " + exportMonth + " 刊";
        ctHeader.setStringValue(headerText);
        XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
        //设置为右对齐
        headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
        XWPFParagraph[] parsHeader = new XWPFParagraph[1];
        parsHeader[0] = headerParagraph;
        policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
//
//        //添加页脚
//        CTP ctpFooter = CTP.Factory.newInstance();
//        CTR ctrFooter = ctpFooter.addNewR();
//        CTText ctFooter = ctrFooter.addNewT();
//        String footerText = "ctpFooter";
//        ctFooter.setStringValue(footerText);
//        XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
//        headerParagraph.setAlignment(ParagraphAlignment.CENTER);
//        XWPFParagraph[] parsFooter = new XWPFParagraph[1];
//        parsFooter[0] = footerParagraph;
//        policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);

        return document;
    }

    /**
     * 1.成果报送及采纳情况
     */
    public void fruitNc(XWPFDocument document, List<FruitPO> list){
        XWPFParagraph zkyjOne = document.createParagraph();
        XWPFRun zkyjRunOne = zkyjOne.createRun();
        zkyjRunOne.setText("1.  成果报送及采纳情况\r");
        zkyjRunOne.setFontSize(12);

        XWPFParagraph bs = document.createParagraph();
        XWPFRun bsRun = bs.createRun();
        bsRun.setText("报送情况");
        bsRun.setFontSize(12);
        //创建一个段落
        XWPFParagraph cp = document.createParagraph();
        //报送表头行
        String[] bsHeadNames = {"序号", "文章名", "作者", "报送部门"};
        //报送数据---获取没有批示， 报送不为空但没有采用的成果
        List<FruitPO> bsFruitList = list.stream()
                .filter(fruitPO -> CollectionUtils.isEmpty(fruitPO.getLeaderPOS()))
                .filter(fruitPO -> !CollectionUtils.isEmpty(fruitPO.getSubList()))
                .filter(fruitPO -> fruitPO.getSubList().stream().allMatch(fruitSubPO -> !fruitSubPO.getIsAdopt()))
                .collect(Collectors.toList());
        XWPFTable bsTable = document.createTable();
        this.headTable(bsTable, bsHeadNames);
        //报送情况表主体行
        this.contentTable(bsTable, bsHeadNames, bsFruitList, 1);

        //换行
        this.addBreak(document);

        XWPFParagraph cy = document.createParagraph();
        XWPFRun cyRun = cy.createRun();
        cyRun.setText("采用情况\r");
        cyRun.setFontSize(12);
        //采用表头行
        String[] cyHeadNames = {"序号", "文章名", "作者", "采用部门", "采用刊物"};
        //采用数据---获取没有批示，报送不为空但采用的成果
        List<FruitPO> cyFruitList = list.stream()
                .filter(fruitPO -> CollectionUtils.isEmpty(fruitPO.getLeaderPOS()))
                .filter(fruitPO -> !CollectionUtils.isEmpty(fruitPO.getSubList()))
                .filter(fruitPO -> fruitPO.getSubList().stream().anyMatch(fruitSubPO -> fruitSubPO.getIsAdopt()))
                .collect(Collectors.toList());
        //采用情况表格
        XWPFTable cyTable = document.createTable();
        this.headTable(cyTable, cyHeadNames);
        //采用情况表主体行
        this.contentTable(cyTable, cyHeadNames, cyFruitList, 2);

        //换行
        this.addBreak(document);

        XWPFParagraph ps = document.createParagraph();
        XWPFRun psRun = ps.createRun();
        psRun.setText("批示情况\r");
        psRun.setFontSize(12);
        //批示表头行
        String[] psHeadNames = {"序号", "文章名", "作者", "采用部门", "采用刊物", "批示领导职务和姓名", "批示类型"};
        //批示数据---获取批示的成果
        List<FruitPO> psFruitList = list.stream()
                .filter(fruitPO -> !CollectionUtils.isEmpty(fruitPO.getLeaderPOS()))
                .collect(Collectors.toList());
        //批示情况表格
        XWPFTable psTable = document.createTable();
        this.headTable(psTable, psHeadNames);
        //批示情况表主体行
        this.contentTable(psTable, psHeadNames, psFruitList, 3);
    }


    /**
     * 2.图书出版
     */
    public void fruitBook(XWPFDocument document, List<FruitPO> list){
        List<FruitPO > tsFruitList = list.stream().filter(fruitPO -> fruitPO.getType().equals(ComConstant.FruitType.BOOK)) .collect(Collectors.toList());

        XWPFParagraph ts = document.createParagraph();
        XWPFRun tsRun = ts.createRun();
        tsRun.setText("2.图书出版\r");
        tsRun.setFontSize(12);

        for (FruitPO fruitPO : tsFruitList) {
            XWPFParagraph tsc = document.createParagraph();
            XWPFRun tscRun = tsc.createRun();
            //书名
            String bookName = fruitPO.getName();
            //作者
            String authorNames = fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining(","));
            //出版社名
            String press = fruitPO.getPress();
            //摘要
            String summary = fruitPO.getSummary();
            tscRun.setText("《"+bookName+"》" + authorNames + "等著，" + press +"。\r" + (!StringUtils.isEmpty(summary) ? summary: "") + "\r");
            tscRun.setFontSize(12);
            //换行
            this.addBreak(document);
        }
    }

    /**
     * 3.成果摘要、收录及获奖
     */
    public void fruitAward(XWPFDocument document, List<FruitPO> list){
        List<FruitPO > tsFruitList = list.stream().filter(fruitPO -> fruitPO.getType().equals(ComConstant.FruitType.PRICE)) .collect(Collectors.toList());

        XWPFParagraph ts = document.createParagraph();
        XWPFRun tsRun = ts.createRun();
        tsRun.setText("3.成果摘要、收录及获奖\r");
        tsRun.setFontSize(12);

        for (FruitPO fruitPO : tsFruitList) {
            XWPFParagraph tsc = document.createParagraph();
            XWPFRun tscRun = tsc.createRun();
            //获奖作品
            String awards = fruitPO.getAwards();
            //奖项
            String name = fruitPO.getName();
            //职务+作者
            String authorNames = fruitPO.getAuthorPOList().stream()
                    .map(fruitAuthorPO -> fruitAuthorPO.getOrgPOList().get(0).getName()+"-"+fruitAuthorPO.getAuthor())
                    .collect(Collectors.joining(","));
            //颁奖单位
            String unit = fruitPO.getGrantUnit();
            tscRun.setText(authorNames + "撰写的《" + awards +"》获得" + unit + "颁发的" + name);
            tscRun.setFontSize(12);
            this.addBreak(document);
        }
    }

    /**
     * 4.文章发表（学术文章和媒体文章）
     */
    public void fruitPost(XWPFDocument document, List<FruitPO> list){

        XWPFParagraph postP = document.createParagraph();
        XWPFRun postRunOne = postP.createRun();
        postRunOne.setText("4.文章发表（学术文章和媒体文章）\r");
        postRunOne.setFontSize(12);

        //创建一个段落
        XWPFParagraph cp = document.createParagraph();
        //表头行
        String[] headNames = {"序号", "文章名", "作者", "发表期刊/媒体", "时间"};
        //成果数据
        List<FruitPO > postFruitList = list.stream().filter(fruitPO -> fruitPO.getType().equals(ComConstant.FruitType.NewspaperORNetwork)
                                                                    || fruitPO.getType().equals(ComConstant.FruitType.Paper)
                                                            ).collect(Collectors.toList());
        //表格
        XWPFTable table = document.createTable();
        this.headTable(table, headNames);
        //表主体行
        this.contentTable(table, headNames, postFruitList, 4);

    }

    /**
     * 5.课题完成
     */
    public void topicData(XWPFDocument document, List<FruitPO> list){

        XWPFParagraph postP = document.createParagraph();
        XWPFRun postRunOne = postP.createRun();
        postRunOne.setText("5.课题完成\r");
        postRunOne.setFontSize(12);

        //创建一个段落
        XWPFParagraph cp = document.createParagraph();
        //表头行
        String[] headNames = {"序号", "课题名称", "课题负责人", "成果名称"};
        //成果数据--研究报告
        List<FruitPO > fruitList = list.stream().filter(fruitPO -> fruitPO.getType().equals(ComConstant.FruitType.REPORT)).collect(Collectors.toList());
        //表格
        XWPFTable table = document.createTable();
        this.headTable(table, headNames);
        //表主体行
        this.contentTable(table, headNames, fruitList, 5);

    }

    /**
     * 专家访谈
     */
    public void fruitInterview(XWPFDocument document, List<FruitPO> list){
        List<FruitPO > ftFruitList = list.stream().filter(fruitPO -> fruitPO.getType().equals(ComConstant.FruitType.NewspaperORNetwork) &&
                                                                     fruitPO.getActivityProdType().equals(ComConstant.FruitType.MEDIA_INTERVIEW))
                                    .collect(Collectors.toList());

        for (FruitPO fruitPO : ftFruitList) {
            //媒体名称
            String organizer = fruitPO.getOrganizer();
            //采访级别
            String awardsLevel = !StringUtils.isEmpty(fruitPO.getAwardsLevelName()) ? fruitPO.getAwardsLevelName() : "";
            //采访日期
            String pubTime = new SimpleDateFormat("yyyy年MM月dd日").format(fruitPO.getPubTime());
            //参访主题
            String name = fruitPO.getOrganizer();
            //受访专家
            String authorNames = fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining(","));
            //专家观点
            String viewPoint = fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor() + ":" + this.getVal(fruitAuthorPO.getViewPoint())).collect(Collectors.joining("\n"));

            XWPFParagraph tsc = document.createParagraph();
            XWPFRun tscRunOrgan = tsc.createRun();
            tscRunOrgan.setText("媒体名称:" + organizer);
            tscRunOrgan.setFontSize(12);

            XWPFParagraph tscLevel = document.createParagraph();
            XWPFRun tscRunLevel = tscLevel.createRun();
            tscRunLevel.setText("采访级别:" + awardsLevel);
            tscRunLevel.setFontSize(12);

            XWPFParagraph tscTime = document.createParagraph();
            XWPFRun tscRunTime = tscTime.createRun();
            tscRunTime.setText("采访日期:" + pubTime);
            tscRunTime.setFontSize(12);

            XWPFParagraph tscName  = document.createParagraph();
            XWPFRun tscRunName = tscName .createRun();
            tscRunName.setText("采访主题:" + name);
            tscRunName.setFontSize(12);

            XWPFParagraph tscAuthor = document.createParagraph();
            XWPFRun tscRunAuthor = tscAuthor.createRun();
            tscRunAuthor.setText("受访专家:" + authorNames);
            tscRunAuthor.setFontSize(12);

            XWPFParagraph tscPoint = document.createParagraph();
            XWPFRun tscRunPoint = tscPoint.createRun();
            tscRunPoint.setText("专家观点:" + this.getVal(viewPoint));
            tscRunPoint.setFontSize(12);
            this.addBreak(document);

        }
    }


    /**
     * 智库活动
     */
    public void fruitMeeting(XWPFDocument document, List<FruitPO> list){
        List<FruitPO > hdFruitList = list.stream().filter(fruitPO -> fruitPO.getType().equals(ComConstant.FruitType.PolicyAdvice) &&
                                                                     fruitPO.getActivityProdType().equals(ComConstant.FruitType.MEETING))
                                    .collect(Collectors.toList());

        for (FruitPO fruitPO : hdFruitList) {
            //会议时间
            String pubTime = new SimpleDateFormat("yyyy年MM月dd日").format(fruitPO.getPubTime());
            //与会专家
            String authorNames = fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining(","));
            //主办单位
            String organizer = fruitPO.getOrganizer();
            //会议名称
            String name = fruitPO.getName();
            //会议摘要
            String summary = fruitPO.getSummary();
            //专家观点
            String viewPoint = fruitPO.getAuthorPOList().stream().filter(fruitAuthorPO -> !StringUtils.isEmpty(fruitAuthorPO.getViewPoint()))
                                                        .map(fruitAuthorPO -> fruitAuthorPO.getAuthor() + ":" + this.getVal(fruitAuthorPO.getViewPoint()))
                                                        .collect(Collectors.joining("\r"));

            XWPFParagraph tsc = document.createParagraph();
            XWPFRun tscRun = tsc.createRun();
            tscRun.setText(pubTime + "，" + authorNames + "参加" + organizer + "主办的" + name + "。");
            tscRun.setFontSize(12);

            XWPFParagraph tsTv = document.createParagraph();
            XWPFRun tstvRun = tsTv.createRun();
            tstvRun.setText(this.getVal(summary) + this.getVal(viewPoint));
            tstvRun.setFontSize(12);

            this.addBreak(document);
        }
    }


    /**
     * 表头行
     */
    public void headTable(XWPFTable table, String[] headNames){

        XWPFTableRow rowHead = table.getRow(0);

        XWPFParagraph cellParagraph = rowHead.getCell(0).getParagraphs().get(0);
        cellParagraph.setAlignment(ParagraphAlignment.CENTER); //设置表头单元格居中
        XWPFRun cellParagraphRun  = cellParagraph.createRun();
        cellParagraphRun.setFontSize(10); //设置表头单元格字号
        cellParagraphRun.setBold(true); //设置表头单元格加粗
        cellParagraphRun.setText(headNames[0]);

        XWPFTableCell headCell = rowHead.getCell(0);
        CTTcPr tcpr = headCell.getCTTc().addNewTcPr();
        CTTblWidth cellw = tcpr.addNewTcW();
        cellw.setType(STTblWidth.DXA);
        cellw.setW(BigInteger.valueOf(500));

        for (int i = 1; i < headNames.length; i++) {
            cellParagraph = rowHead.addNewTableCell().getParagraphs().get(0);
            cellParagraph.setAlignment(ParagraphAlignment.CENTER); //设置表头单元格居中
            cellParagraphRun  = cellParagraph.createRun();
            cellParagraphRun.setFontSize(10); //设置表头单元格居中
            cellParagraphRun.setBold(true); //设置表头单元格加粗
            cellParagraphRun.setText(headNames[i]);
        }
    }

    /**
     * 换行
     * @param document
     */
    public void addBreak(XWPFDocument document){
        XWPFParagraph xp = document.createParagraph();
        XWPFRun hhr = xp.createRun();
        hhr.setText("\r");
    }

    /**
     * 表主题行
     */
    public void contentTable(XWPFTable table, String[] headNames, List<FruitPO> list, int type){
        //表格属性
        for (int i = 0; i < list.size(); i++) {
            XWPFTableRow contentRow = table.createRow();
            FruitPO fruitPO = list.get(i);
            //循环插入值
            for (int j = 0; j < headNames.length; j++) {
                XWPFParagraph cellParagraphC = contentRow.getCell(j).getParagraphs().get(0);
                cellParagraphC.setAlignment(ParagraphAlignment.CENTER); //设置表格内容居中

                XWPFRun cellParagraphRunC  = cellParagraphC.createRun();
                cellParagraphRunC.setFontSize(10); //设置表格内容字号
                cellParagraphRunC.setText(
                        type == 1 ? this.setBsVal(fruitPO, i, j):
                        type == 2 ? this.setCyVal(fruitPO, i, j):
                        type == 3 ? this.setPsVal(fruitPO, i, j):
                        type == 4 ? this.setPostVal(fruitPO, i, j):
                        type == 5 ? this.setReportVal(fruitPO, i, j): ""
                );
            }
        }
    }

    //设置报送表格内容值
    public String setBsVal(FruitPO fruitPO, int x, int y){
        return  y==0 ? x+1+"" :
                y==1 ? fruitPO.getName() :
                y==2 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining(",")) :
                y==3 ? fruitPO.getSubList().stream().filter(fruitSubPO -> !StringUtils.isEmpty(fruitSubPO.getSubDeparment())).map(fruitSubPO -> fruitSubPO.getSubDeparment()).collect(Collectors.joining(","))
                     : "";
    }

    //设置采用表格内容值
    public String setCyVal(FruitPO fruitPO, int x, int y){
        return  y==0 ? x+1+"" :
                y==1 ? fruitPO.getName() :
                y==2 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining(",")) :
                y==3 ? fruitPO.getSubList().stream().map(fruitSubPO -> fruitSubPO.getSubDeparment()).collect(Collectors.joining(",")) :
                y==4 ? fruitPO.getSubList().stream().filter(fruitSubPO -> !StringUtils.isEmpty(fruitSubPO.getAdoptJournal())).map(fruitSubPO -> fruitSubPO.getAdoptJournal()).collect(Collectors.joining(","))
                     : "";
    }


    //设置批示表格内容值
    public String setPsVal(FruitPO fruitPO, int x, int y){
        return  y==0 ? x+1+"" :
                y==1 ? fruitPO.getName() :
                y==2 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining(",")) :
                y==3 ? fruitPO.getSubList().stream().map(fruitSubPO -> fruitSubPO.getSubDeparment()).collect(Collectors.joining(",")) :
                y==4 ? fruitPO.getSubList().stream().map(fruitSubPO -> fruitSubPO.getAdoptJournal()).collect(Collectors.joining(",")) :
                y==5 ? fruitPO.getLeaderPOS().stream().map(fruitLeaderPO -> fruitLeaderPO.getJob()+ ":" + fruitLeaderPO.getName()).collect(Collectors.joining(",")) :
                y==6 ? fruitPO.getLeaderPOS().stream().filter(fruitSubPO -> !StringUtils.isEmpty(fruitSubPO.getInclinationName())).map(fruitLeaderPO -> fruitLeaderPO.getInclinationName()).collect(Collectors.joining(","))
                     : "";
    }

    //设置文章数据
    public String setPostVal(FruitPO fruitPO, int x, int y){
        return  y==0 ? x+1+"" :
                y==1 ? fruitPO.getName() :
                y==2 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining(",")) :
                y==3 ? fruitPO.getJournalName():
                y==4 ? new SimpleDateFormat("yyyy年MM月dd日").format(fruitPO.getPubTime())
                     : "";
    }
    //设置研究报告数据
    public String setReportVal(FruitPO fruitPO, int x, int y){
        return  y==0 ? x+1+"" :
                y==1 ? fruitPO.getName() :
                y==2 ? fruitPO.getAuthorPOList().get(0).getAuthor()
                     : "";
    }

    public String getVal(String val){
        return !StringUtils.isEmpty(val) ? val : "";
    }



    public XWPFDocument exportWorkSummaryFruitReportData(String exportMonth, OrgPo orgPo, List<FruitPO> listFruit) throws IOException {

        XWPFDocument document = new XWPFDocument();

        //添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText(exportMonth + "工作总结成果统计");
        titleParagraphRun.setFontSize(20);

        //批示不为空
        List<FruitPO> list = listFruit.stream().filter(fruitPO -> !CollectionUtils.isEmpty(fruitPO.getLeaderPOS())).collect(Collectors.toList());
        //批示为空 报送不为空
        List<FruitPO> listOne = listFruit.stream().filter(fruitPO -> CollectionUtils.isEmpty(fruitPO.getLeaderPOS()) && !CollectionUtils.isEmpty(fruitPO.getSubList())).collect(Collectors.toList());
        //都为空
        List<FruitPO> listTwo = listFruit.stream().filter(fruitPO -> CollectionUtils.isEmpty(fruitPO.getLeaderPOS()) && CollectionUtils.isEmpty(fruitPO.getSubList())).collect(Collectors.toList());
        list.addAll(listOne);
        list.addAll(listTwo);
        //换行
        this.addBreak(document);
        //1.咨政内参
        String[] ncHeaderName = {"序号", "题目", "作者", "刊登情况", "批示及应用转化情况"};
        this.workFruit("决策咨询成果统计表", ncHeaderName, ComConstant.FruitType.InternalReferenceReport, null, document, list);
        //换行
        this.addBreak(document);
        //2.学术论文
        String[] lwHeaderName = {"序号", "题目", "作者", "期刊名称"};
        this.workFruit("学术论文成果统计表", lwHeaderName, ComConstant.FruitType.Paper, null, document, list);
        //换行
        this.addBreak(document);
        //3.研究报告
        String[] bgHeaderName = {"序号", "题目", "作者", "刊登情况", "批示及应用转化情况"};
        this.workFruit("研究报告成果统计表", bgHeaderName, ComConstant.FruitType.REPORT, null, document, list);
        //换行
        this.addBreak(document);
        //4.图书出版
        String[] tsHeaderName = {"序号", "书名", "作者", "出版社"};
        this.workFruit("图书出版成果统计表", tsHeaderName, ComConstant.FruitType.BOOK, null, document, list);
        //换行
        this.addBreak(document);
        //5.活动成果--出席会议
        String[] hyHeaderName = {"序号", "会议名称", "会议专家", "会议时间"};
        this.workFruit("活动会议成果统计表", hyHeaderName, ComConstant.FruitType.PolicyAdvice, ComConstant.FruitType.MEETING, document, list);
        //5.媒体成果--接受采访
        String[] cfHeaderName = {"序号", "采访主题", "受访专家", "采访媒体", "会议时间"};
        this.workFruit("活动采访成果统计表", cfHeaderName, ComConstant.FruitType.NewspaperORNetwork, ComConstant.FruitType.MEDIA_INTERVIEW, document, list);
        //换行
        this.addBreak(document);
        //6.媒体成果
        String[] mtHeaderName = {"序号", "标题", "作者", "日期", "刊物"};
        this.workFruit("媒体成果统计表", mtHeaderName, ComConstant.FruitType.NewspaperORNetwork, null, document, list);
        //换行
        this.addBreak(document);
        //7.荣誉信息
        String[] ryHeaderName = {"序号", "获奖名次", "获奖作品", "作者", "颁奖单位"};
        this.workFruit("荣誉信息成果统计表", ryHeaderName, ComConstant.FruitType.PRICE, null, document, list);
        //换行
        this.addBreak(document);
        //TODO 8. 省重点课题
        String[] ktHeaderName = {"序号", "课题名称", "负责人", "交办单位"};
        //.......

        return document;
    }

    private void workFruit(String title, String[] headNames, String type, String activityProdType, XWPFDocument document, List<FruitPO> list) {

        XWPFParagraph postP = document.createParagraph();
        XWPFRun postRunOne = postP.createRun();
        postRunOne.setText(title + "\r");
        postRunOne.setFontSize(12);

        //成果数据
        List<FruitPO > postFruitList = list.stream().filter(fruitPO -> fruitPO.getType().equals(type)).collect(Collectors.toList());
        //表格
        XWPFTable table = document.createTable();
        this.headTable(table, headNames);
        //表主体行
        this.summaryContentTable(table, headNames, postFruitList, type, activityProdType);

    }

    /**
     * 工作总结 表主题行
     */
    public void summaryContentTable(XWPFTable table, String[] headNames, List<FruitPO> list, String type, String activityProdType){
        //表格属性
        for (int i = 0; i < list.size(); i++) {
            FruitPO fruitPO = list.get(i);
            //只查询活动的会议和采访
            if(ComConstant.FruitType.PolicyAdvice.equals(type) && (!activityProdType.equals(fruitPO.getActivityProdType()))){
                continue;
            }else {
                XWPFTableRow contentRow = table.createRow();
                //循环插入值
                for (int j = 0; j < headNames.length; j++) {
                    XWPFParagraph cellParagraphC = contentRow.getCell(j).getParagraphs().get(0);
                    cellParagraphC.setAlignment(ParagraphAlignment.CENTER); //设置表格内容居中

                    XWPFRun cellParagraphRunC = cellParagraphC.createRun();
                    cellParagraphRunC.setFontSize(10); //设置表格内容字号
                    cellParagraphRunC.setText(
                            ComConstant.FruitType.InternalReferenceReport.equals(type) ? this.setPsSummaryVal(fruitPO, i, j) :
                            ComConstant.FruitType.Paper.equals(type) ? this.setPaPerSummaryVal(fruitPO, i, j) :
                            ComConstant.FruitType.REPORT.equals(type) ? this.setPsSummaryVal(fruitPO, i, j) :
                            ComConstant.FruitType.BOOK.equals(type) ? this.setTsSummaryVal(fruitPO, i, j) :
                            ComConstant.FruitType.PolicyAdvice.equals(type) ? this.setHdSummaryVal(fruitPO, i, j, fruitPO.getActivityProdType(), activityProdType) :
                            ComConstant.FruitType.NewspaperORNetwork.equals(type) ? this.setMtSummaryVal(fruitPO, i, j) :
                            ComConstant.FruitType.PRICE.equals(type) ? this.setRySummaryVal(fruitPO, i, j) :
                            ComConstant.FruitType.PROJECT.equals(type) ? this.setProjectSummaryVal(fruitPO, i, j) :
                            ""
                    );
                }
            }
        }
    }

    private String setPsSummaryVal(FruitPO fruitPO, int x, int y) {
        return  y==0 ? x+1+"" :
                y==1 ? fruitPO.getName() :
                y==2 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining("、")) :
                y==3 ? fruitPO.getSubList().stream().filter(fruitSubPO -> !StringUtils.isEmpty(fruitSubPO.getAdoptJournal())).map(fruitSubPO -> fruitSubPO.getAdoptJournal()).collect(Collectors.joining("、")) :
                y==4 ? getPsData(fruitPO)
                : "";
    }

    //获取采用和批示数据
    private String getPsData(FruitPO fruitPO){
        return  fruitPO.getLeaderPOS().stream()
                .map(fruitLeaderPO -> (!StringUtils.isEmpty(fruitLeaderPO.getUnit())?fruitLeaderPO.getUnit():"") + " " +
                                      (!StringUtils.isEmpty(fruitLeaderPO.getJob())?fruitLeaderPO.getJob():"") + " " +
                                      (!StringUtils.isEmpty(fruitLeaderPO.getName())?fruitLeaderPO.getName():"") + " " + "批示")
                .collect(Collectors.joining("\n"));
    }

    private String setPaPerSummaryVal(FruitPO fruitPO, int x, int y) {
        return  y==0 ? x+1+"" :
                y==1 ? fruitPO.getName() :
                y==2 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining("、")) :
                y==3 ? fruitPO.getJournalName()
                : "";
    }

    private String setTsSummaryVal(FruitPO fruitPO, int x, int y) {
        return  y==0 ? x+1+"" :
                y==1 ? fruitPO.getName() :
                y==2 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining("、")) :
                y==3 ? fruitPO.getPress()
                : "";
    }

    private String setHdSummaryVal(FruitPO fruitPO, int x, int y, String fruitActivityProdType, String wordActivityProdType) {
        if(fruitActivityProdType.equals(wordActivityProdType)){
            if(ComConstant.FruitType.MEETING.equals(wordActivityProdType)){
                return  y==0 ? x+1+"" :
                        y==1 ? fruitPO.getName() :
                        y==2 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining("、")) :
                        y==3 ? new SimpleDateFormat("yyyy-MM-dd").format(fruitPO.getPubTime())
                        : "";
            }else if(ComConstant.FruitType.RECEIVE_VISITORS.equals(wordActivityProdType)){
                return  y==0 ? x+1+"" :
                        y==1 ? fruitPO.getName() :
                        y==2 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining("、")) :
                        y==3 ? fruitPO.getOrganizer() :
                        y==4 ? new SimpleDateFormat("yyyy-MM-dd").format(fruitPO.getPubTime())
                        : "";
            }
        }
        return "";
    }

    private String setMtSummaryVal(FruitPO fruitPO, int x, int y) {
        return  y==0 ? x+1+"" :
                y==1 ? fruitPO.getName() :
                y==2 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining("、")) :
                y==3 ? new SimpleDateFormat("yyyy-MM-dd").format(fruitPO.getPubTime()) :
                y==4 ? fruitPO.getJournalName()
                : "";
    }

    private String setRySummaryVal(FruitPO fruitPO, int x, int y) {
        return  y==0 ? x+1+"" :
                y==1 ? fruitPO.getName() :
                y==2 ? fruitPO.getAwards() :
                y==3 ? fruitPO.getAuthorPOList().stream().map(fruitAuthorPO -> fruitAuthorPO.getAuthor()).collect(Collectors.joining("、")) :
                y==4 ? fruitPO.getGrantUnit()
                : "";

    }

    private String setProjectSummaryVal(FruitPO fruitPO, int x, int y) {
        return "";
    }
}